<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        style="width: 200px;"
        class="filter-item"
        placeholder="用户姓名"
        v-model="queryForm.userName"
      ></el-input>
      <el-input
        style="width: 200px;"
        class="filter-item"
        placeholder="手机号码"
        v-model="queryForm.phone"
      ></el-input>
      <el-input
        style="width: 200px;"
        class="filter-item"
        placeholder="业务单号"
        v-model="queryForm.voucherCode"
      ></el-input>
      <el-select
        clearable
        style="width: 170px"
        class="filter-item"
        v-model="queryForm.type"
        placeholder="请选择广告类型"
      >
        <el-option
          v-for="(val, key) in Dicts.AD_ORDER_TYPE"
          :key="key"
          :label="val"
          :value="key"
        ></el-option>
      </el-select>
      <el-select
        clearable
        style="width: 170px"
        class="filter-item"
        v-model="queryForm.status"
        placeholder="请选择审核状态"
      >
        <el-option
          v-for="(val, key) in Dicts.AD_ORDER_STATUS"
          :key="key"
          :label="val"
          :value="key"
        ></el-option>
      </el-select>
      <el-button
        class="filter-item"
        @click="doQuery"
        type="primary"
        icon="el-icon-search"
        >查询</el-button
      >
    </div>
    <el-table v-loading="loading" :data="records" style="width: 100%">
      <el-table-column prop="rowId" label="ID" width="100" align="center">
      </el-table-column>
      <el-table-column
        prop="userName"
        label="用户姓名"
        width="150"
        align="center"
      >
      </el-table-column>
      <el-table-column prop="phone" label="手机号" width="150" align="center">
      </el-table-column>
      <el-table-column
        prop="companyName"
        label="企业名称"
        width="250"
        align="center"
      >
      </el-table-column>
      <el-table-column prop="type" label="广告类型" width="150" align="center">
        <template slot-scope="scope">
          {{ Dicts.AD_ORDER_TYPE[scope.row.type] }}
        </template>
      </el-table-column>
      <el-table-column
        prop="voucherCode"
        label="业务单号"
        width="250"
        align="center"
      >
      </el-table-column>
      <el-table-column prop="title" label="广告标题" width="300" align="center">
      </el-table-column>
      <el-table-column
        prop="orderTime"
        label=" 下单时间"
        width="200"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="orderAmount"
        label="订单金额"
        width="200"
        align="center"
      >
        <template slot-scope="scope">
          {{ `￥${scope.row.orderAmount}` }}
        </template>
      </el-table-column>
      <el-table-column
        prop="status"
        label="订单状态"
        width="150"
        align="center"
      >
        <template slot-scope="scope">
          {{ Dicts.AD_ORDER_STATUS[scope.row.status] }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="操作"
        class-name="small-padding"
        fixed="right"
        width="250"
      >
        <template slot-scope="scope">
          <el-button
            v-permission="'userAdOrder:detail'"
            size="mini"
            @click="detailsOrder(scope.row)"
            title="详情"
            icon="el-icon-search"
            circle
          ></el-button>
          <el-button
            v-permission="'userAdOrder:checkOrder'"
            size="mini"
            v-if="scope.row.status == 2 ? true : false"
            type="primary"
            @click="checkOrder(scope.row)"
            title="审核"
            icon="el-icon-edit"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryForm.current"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="queryForm.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="queryForm.total"
      >
      </el-pagination>
    </div>

    <!-- 审核 -->
    <el-dialog
      :title="`审核用户广告订单`"
      :visible.sync="checkDialogFormVisible"
      width="46%"
    >
      <el-form
        :model="checkForm"
        label-width="140px"
        :rules="checkFormRules"
        ref="checkForm"
      >
        <el-row>
          <el-col :span="20">
            <el-form-item label="广告标题:" prop="title">
              {{ checkForm.title }}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="10">
            <el-form-item label="用户姓名:" prop="userName">
              {{ checkForm.userName }}
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="业务单号:" prop="voucherCode">
              {{ checkForm.voucherCode }}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="10">
            <el-form-item label="广告类型:" prop="type">
              {{ Dicts.AD_ORDER_TYPE[checkForm.type] }}
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="企业名称:" prop="companyName">
              {{ checkForm.companyName }}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="10">
            <el-form-item label="订单时间:" prop="orderTime">
              {{ checkForm.orderTime }}
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="支付方式:" prop="payType">
              {{ Dicts.PAY_TYPE[checkForm.payType] }}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="10">
            <el-form-item label="订单金额:" prop="orderAmount">
              {{ checkForm.orderAmount }}
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="实付金额:" prop="realpayAmount">
              {{ checkForm.realpayAmount }}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="10">
            <el-form-item label="图片:" prop="photo">
              <template slot-scope="scope">
                <img
                  :src="$oss(checkForm.photo)"
                  alt
                  style="width: 280px;height: 170px"
                />
              </template>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="10">
            <el-form-item label="审核结果:" prop="checkResult">
              <el-select
                v-model="checkForm.checkResult"
                placeholder="审核结果"
                style="width: 100%;"
              >
                <el-option
                  v-for="(val, key) in Dicts.AD_CHECK_RESULT"
                  :key="key"
                  :label="val"
                  :value="key"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="20">
            <el-form-item label="审核说明:" prop="remark">
              <el-input
                v-model="checkForm.remark"
                type="textarea"
                :rows="3"
                auto-complete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="checkDialogFormVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="checkMdUserAdOrder(checkForm)"
          v-show="Button"
          >确 定</el-button
        >
      </div>
    </el-dialog>

    <!-- 详情 -->
    <el-dialog
      :title="`详情`"
      :visible.sync="detailsDialogFormVisible"
      width="46%"
    >
      <el-form :model="checkForm" label-width="140px" ref="checkForm">
        <el-row>
          <el-col :span="20">
            <el-form-item label="广告标题:" prop="title">
              {{ checkForm.title }}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="10">
            <el-form-item label="用户姓名:" prop="userName">
              {{ checkForm.userName }}
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="业务单号:" prop="voucherCode">
              {{ checkForm.voucherCode }}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="10">
            <el-form-item label="广告类型:" prop="type">
              {{ Dicts.AD_ORDER_TYPE[checkForm.type] }}
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="企业名称:" prop="companyName">
              {{ checkForm.companyName }}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="10">
            <el-form-item label="订单时间:" prop="orderTime">
              {{ checkForm.orderTime }}
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="支付方式:" prop="payType">
              {{ Dicts.PAY_TYPE[checkForm.payType] }}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="10">
            <el-form-item label="订单金额:" prop="orderAmount">
              {{ checkForm.orderAmount }}
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="实付金额:" prop="realpayAmount">
              {{ checkForm.realpayAmount }}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="10">
            <el-form-item label="图片:" prop="photo">
              <template slot-scope="scope">
                <img
                  :src="$oss(checkForm.photo)"
                  alt
                  style="width: 280px;height: 170px"
                />
              </template>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="10">
            <el-form-item label="审核人:" prop="checkName">
              {{ checkForm.checkName }}
            </el-form-item>
          </el-col>

          <el-col :span="10">
            <el-form-item label="审核时间:" prop="checkTime">
              {{ checkForm.checkTime }}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="10">
            <el-form-item label="审核结果:" prop="checkResult">
              {{ Dicts.AD_CHECK_RESULT[checkForm.checkResult] }}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="20">
            <el-form-item label="审核说明:" prop="remark">
              {{ checkForm.checkRemark }}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  queryUserAdOrder,
  checkMdUserAdOrder,
  detailsMdUserAdOrder
} from '@/api/ad'

export default {
  name: 'userAdOrder',
  data() {
    return {
      checkDialogFormVisible: false,
      detailsDialogFormVisible: false,
      Button: true,
      loading: false,
      queryForm: {
        current: 1,
        pages: 0,
        total: 0,
        size: 10,
        title: undefined
      },
      checkForm: {
        rowId: 0,
        userName: '',
        voucherCode: '',
        type: '',
        companyName: '',
        title: '',
        photo: '',
        orderTime: '',
        payTime: '',
        orderAmount: '',
        realpayAmount: '',
        lastPayTime: '',
        status: '',
        checkName: '',
        checkTime: '',
        checkResult: '',
        remark: '',
        payType: '',
        externalTransno: ''
      },
      checkFormRules: {
        checkResult: [
          { required: true, message: '请选择审核结果', trigger: 'blur' }
        ],
        remark: [{ required: true, message: '请填写审核说明', trigger: 'blur' }]
      },
      records: []
    }
  },
  created() {
    // 初始化一次oss client,如果初始化过一次返回上一次初始化对象
    this.initOssClient().then(_ => {
      this.doQuery()
    })
  },
  methods: {
    handleCurrentChange(current) {
      this.queryForm.current = current
      this.doQuery()
    },
    handleSizeChange(size) {
      this.queryForm.size = size
      this.doQuery()
    },
    doQuery() {
      this.loading = true
      queryUserAdOrder(this.queryForm).then(({ data: { data } }) => {
        this.queryForm.current = data.current
        this.queryForm.pages = data.pages
        this.queryForm.total = data.total
        this.queryForm.size = data.size
        this.records = data.records
        this.loading = false
      })
    },
    checkOrder(row) {
      this.Button = true
      this.checkForm = {
        rowId: row.rowId,
        userName: row.userName,
        voucherCode: row.voucherCode,
        type: row.type,
        companyName: row.companyName,
        title: row.title,
        photo: row.photo,
        orderTime: row.orderTime,
        payTime: row.payTime,
        orderAmount: row.orderAmount,
        realpayAmount: row.realpayAmount,
        lastPayTime: row.lastPayTime,
        status: row.status,
        payType: row.payType,
        externalTransno: row.externalTransno
      }
      this.checkDialogFormVisible = true
      this.$refs.checkForm && this.$refs.checkForm.resetFields()
    },
    checkMdUserAdOrder() {
      this.$refs.checkForm.validate(valid => {
        if (valid) {
          this.Button = false
          checkMdUserAdOrder(this.checkForm).then(
            ({ data: { data, msg, code } }) => {
              if (code === 0) {
                this.doQuery()
                this.checkDialogFormVisible = false
              }
              this.$message({
                showClose: true,
                message: msg,
                type: code === 0 ? 'success' : 'error'
              })
            }
          )
        } else {
          return false
        }
      })
    },
    detailsOrder(row) {
      this.detailsDialogFormVisible = true
      detailsMdUserAdOrder(row.rowId).then(({ data: { code, data, msg } }) => {
        this.checkForm = {
          rowId: data.rowId,
          userName: data.userName,
          voucherCode: data.voucherCode,
          type: data.type,
          companyName: row.companyName,
          title: data.title,
          photo: data.photo,
          orderTime: data.orderTime,
          payTime: data.payTime,
          orderAmount: data.orderAmount,
          realpayAmount: data.realpayAmount,
          lastPayTime: data.lastPayTime,
          status: data.status,
          payType: data.payType,
          externalTransno: data.externalTransno,
          checkResult: data.checkResult,
          checkRemark: data.checkRemark,
          checkTime: data.checkTime,
          checkName: data.checkName
        }
      })
    }
  },
  components: {}
}
</script>

<style scoped></style>
